//
//  Popup.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/21.
//

import Foundation
import SwiftEntryKit
import RxSwift
import RxCocoa
class Popup {
    
   static   let shared = Popup()
    var observable = PublishSubject<String>()
     func showPopupMessage(attributes: EKAttributes,
                                  title: String,
                                  titleColor: EKColor,
                                  description: String,
                                  descriptionColor: EKColor,
                                  buttonTitleColor: EKColor,
                                  buttonBackgroundColor: EKColor,
                                  image: UIImage? = nil) {
        
        var themeImage: EKPopUpMessage.ThemeImage?
        
        if let image = image {
            themeImage = EKPopUpMessage.ThemeImage(
                image: EKProperty.ImageContent(
                    image: image,
                   displayMode: PresetsDataSource.displayMode,
                    size: CGSize(width: 60, height: 60),
                    tint: titleColor,
                    
                    contentMode: .scaleAspectFit
                )
            )
        }
        let title = EKProperty.LabelContent(
            text: title,
            style: .init(
                font: .adaptedBoldFont(size: 20),
                color: titleColor,
                alignment: .center,
                displayMode: PresetsDataSource.displayMode
            ),
            accessibilityIdentifier: "title"
        )
        let description = EKProperty.LabelContent(
            text: description,
            style: .init(
                font: .adaptedBoldFont(size: 20),
                color: descriptionColor,
                alignment: .center,
                displayMode: PresetsDataSource.displayMode
            ),
            accessibilityIdentifier: "description"
        )
        let button = EKProperty.ButtonContent(
            label: .init(
                text: "Got it!",
                style: .init(
                    font: .adaptedBoldFont(size: 20),
                    color: buttonTitleColor,
                    displayMode: PresetsDataSource.displayMode
                )
            ),
            backgroundColor: buttonBackgroundColor,
            highlightedBackgroundColor: buttonTitleColor.with(alpha: 0.05),
            displayMode: PresetsDataSource.displayMode,
            accessibilityIdentifier: "button"
        )
        let message = EKPopUpMessage(
            themeImage: themeImage,
            title: title,
            description: description,
            button: button) { [self] in
                SwiftEntryKit.dismiss()
                
                observable.onNext("callback")
                
//                let result = PKResultImageListViewController()
//                AppContext.shared.topViewController?.navigationController?.pushViewController(result, animated: true)
   
                
        }
        let contentView = EKPopUpMessageView(with: message)
        SwiftEntryKit.display(entry: contentView, using: attributes,presentInsideKeyWindow: true)
    }
    
         func showNotificationMessage(attributes: EKAttributes,
                                             title: String,
                                             desc: String,
                                             textColor: EKColor,
                                             imageName: String? = nil) {
            let title = EKProperty.LabelContent(
                text: title,
                style: .init(
                    font: .adaptedBoldFont(size: 15),
                    color: textColor,
                    displayMode: displayMode
                ),
                accessibilityIdentifier: "title"
            )
            let description = EKProperty.LabelContent(
                text: desc,
                style: .init(
                    font: .adaptedBoldFont(size: 15),
                    color: textColor,
                    displayMode: displayMode
                ),
                accessibilityIdentifier: "description"
            )
            var image: EKProperty.ImageContent?
            if let imageName = imageName {
                image = EKProperty.ImageContent(
                    image: UIImage(named: imageName)!.withRenderingMode(.alwaysTemplate),
                    displayMode: displayMode,
                    size: CGSize(width: 35, height: 35),
                    tint: textColor,
                    accessibilityIdentifier: "thumbnail"
                )
            }
            let simpleMessage = EKSimpleMessage(
                image: image,
                title: title,
                description: description
            )
            let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
            let contentView = EKNotificationMessageView(with: notificationMessage)
            SwiftEntryKit.display(entry: contentView, using: attributes)
        }
    
        private var displayMode: EKAttributes.DisplayMode {
            return PresetsDataSource.displayMode
        }
    
    func  attributes() ->  EKAttributes   {
        var attributes: EKAttributes
        attributes = .topFloat
        attributes.displayMode = PresetsDataSource.displayMode
        attributes.displayDuration = 1
        attributes.hapticFeedbackType = .success
        attributes.entryBackground = .gradient(
            gradient: .init(
                colors: [.amber, .pinky],
                startPoint: .zero,
                endPoint: CGPoint(x: 1, y: 1)
            )
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.5),
                scale: .init(from: 1, to: 0.7, duration: 0.7)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.5,
                radius: 10
            )
        )
        attributes.statusBar = .dark
        attributes.scroll = .enabled(
            swipeable: true,
            pullbackAnimation: .easeOut
        )
        attributes.positionConstraints.maxSize = .init(
            width: .constant(value: UIScreen.main.minEdge),
            height: .intrinsic
        )
        return attributes
    }
    
    
    func notesAttributes() -> EKAttributes {
        var attributes: EKAttributes
        attributes = .topNote
        attributes.displayMode = PresetsDataSource.displayMode
        attributes.hapticFeedbackType = .success
        attributes.popBehavior = .animated(animation: .translation)
        attributes.entryBackground = .color(color: .satCyan)
        attributes.shadow = .active(
            with: .init(
                color: .chatMessage,
                opacity: 0.5,
                radius: 2
            )
        )
        attributes.statusBar = .light
        attributes.lifecycleEvents.willAppear = {
            print("will appear action goes here")
        }
        attributes.lifecycleEvents.didAppear = {
            print("did appear action goes here")
        }
        attributes.lifecycleEvents.willDisappear = {
            print("will disappear action goes here")
        }
        attributes.lifecycleEvents.didDisappear = {
            print("did disappear action goes here")
        }
//        descriptionString = "Absorbs (swallows) touches and the status bar becomes light"
//        descriptionThumb = ThumbDesc.topNote.rawValue
//        description = .init(
//            with: attributes,
//            title: "Top Standard Note",
//            description: descriptionString,
//            thumb: descriptionThumb
//        )
        return attributes
    }
    
}
